package com.tebiecloud.mpweixin.server.controller.feign;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.JsonUtils;
import com.tebiecloud.common.utils.NetworkUtils;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.mpweixin.client.constants.MpminiConstants;
import com.tebiecloud.mpweixin.client.model.entity.MpminiUser;
import com.tebiecloud.mpweixin.client.service.IMpminiApiServiceClient;
import com.tebiecloud.mpweixin.server.properties.MpminiProperties;
import com.tebiecloud.mpweixin.server.service.MpminiUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 小程序接口
 * @Author: tebie
 * @Date: 2019-09-24 17:09
 */
@Slf4j
@RestController
@RequestMapping("/feign/mpminiapi")
public class MiniApiController implements IMpminiApiServiceClient {

    @Autowired
    private MpminiProperties mpminiProperties;

    @Autowired
    private MpminiUserService mpminiUserService;

    /**
     * 小程序授权
     * @param code
     * @return
     */
    @PostMapping("infoMpminiUser")
    @Override
    public ResultBody<MpminiUser> getMpminiUser(
            @RequestParam(value = "nickame",required = false) String nickName,
            @RequestParam(value = "avatarUrl",required = false) String avatarUrl,
            @RequestParam("code") String code) {
        String appid = mpminiProperties.getAppid();
        String secret = mpminiProperties.getSecret();

        if(!StringUtils.isEmpty(appid) && !StringUtils.isEmpty(secret) && !StringUtils.isEmpty(code)) {
            String url = String.format(MpminiConstants.SNS_JSCODE2SESSION, appid, secret, code);
            String resultStr = NetworkUtils.doGet(url);
            MpminiUser mpminiUser = JsonUtils.str2Bean(resultStr, MpminiUser.class);

            // 拉取粉丝信息
            if(mpminiUser != null && !StringUtils.isEmpty(mpminiUser.getOpenId())) {
                MpminiUser mpminiUserOld = mpminiUserService.getMpminiUser(mpminiUser.getOpenId());
                if(mpminiUserOld == null) {
                    // 添加数据库
                    mpminiUser.setGender(0);
                    mpminiUser.setAvatarUrl(avatarUrl);
                    mpminiUser.setNickName(nickName);
                    mpminiUser.setLastVisit(new Date());
                    mpminiUserService.save(mpminiUser);
                    return ResultBodyUtils.ok(mpminiUser);
                } else {
                    if(!StringUtils.isEmpty(nickName) && !StringUtils.isEmpty(avatarUrl)){
                        UpdateWrapper<MpminiUser> mpminiUserUpdateWrapper = new UpdateWrapper<>();
                        mpminiUserUpdateWrapper.eq("mpmini_user_id",mpminiUserOld.getMpminiUserId());
                        mpminiUserOld.setNickName(nickName);
                        mpminiUserOld.setAvatarUrl(avatarUrl);
                        mpminiUserService.update(mpminiUserOld,mpminiUserUpdateWrapper);
                    }
                    return ResultBodyUtils.ok(mpminiUserOld);
                }
            }
        } else {
            log.error("xiaoc>> 小程序授权获取粉丝信息失败，错误的参数, appid: {}, secret: {}, code: {}", appid, secret, code);
        }

        return ResultBodyUtils.failed();
    }

    @PostMapping("getMpminiUserByOpenid")
    @Override
    public ResultBody<MpminiUser> getMpminiUserByOpenid(String openid) {
        QueryWrapper<MpminiUser> mpminiUserQueryWrapper = new QueryWrapper<>();
        mpminiUserQueryWrapper.eq("open_id",openid);
        MpminiUser mpminiUser = mpminiUserService.getOne(mpminiUserQueryWrapper);
        return ResultBodyUtils.ok(mpminiUser);
    }
}
